package com.ruoyi.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.business.entity.YytMember;
import com.ruoyi.common.pms.service.YytPmsSkuInfoService;
import com.ruoyi.common.sms.entity.YytSmsCoupon;
import com.ruoyi.common.sms.entity.YytSmsCouponHistory;
import com.ruoyi.common.sms.entity.YytSmsCouponSpuRelation;
import com.ruoyi.common.sms.service.YytSmsCouponHistoryService;
import com.ruoyi.common.sms.service.YytSmsCouponService;
import com.ruoyi.common.sms.service.YytSmsCouponSpuRelationService;
import com.ruoyi.common.ums.entity.YytUmsMemberCollectSpu;
import com.ruoyi.common.ums.service.YytUmsMemberCollectSpuService;
import com.ruoyi.utils.LoginUtils;
import com.ruoyi.utils.RedisService;
import com.ruoyi.utils.ResponseResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户对商品的操作
 *
 * @author tanxingxian
 * @date 2021-12-22 15:57
 */
@RestController
@RequestMapping("/memberSku")
public class MemberSkuController {

    @Autowired
    private RedisService redisService;
    @Autowired
    private YytPmsSkuInfoService yytPmsSkuInfoService;
    @Autowired
    private YytSmsCouponService yytSmsCouponService;
    @Autowired
    private YytSmsCouponHistoryService yytSmsCouponHistoryService;
    @Autowired
    private YytSmsCouponSpuRelationService yytSmsCouponSpuRelationService;
    @Autowired
    private YytUmsMemberCollectSpuService yytUmsMemberCollectSpuService;

    /**
     * 收藏（取消收藏商品）
     */
    @PostMapping("/collect")
    public ResponseResult collect(@RequestBody YytUmsMemberCollectSpu collectSpu) {
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        collectSpu.setMemberId(loginUser.getId());
        return ResponseResult.success(yytUmsMemberCollectSpuService.collect(collectSpu));
    }

    /**
     * 是否已收藏商品
     */
    @GetMapping("/isCollected")
    public ResponseResult isCollected(@RequestParam("skuId") String skuId) {
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        LambdaQueryWrapper<YytUmsMemberCollectSpu> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YytUmsMemberCollectSpu::getMemberId, loginUser.getId());
        wrapper.eq(YytUmsMemberCollectSpu::getSkuId, skuId);
        return ResponseResult.success(null != yytUmsMemberCollectSpuService.getOne(wrapper));
    }

    /**
     * 获取用户已收藏商品列表
     */
    @GetMapping("/collectedList")
    public ResponseResult collectedList() {
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        List<YytUmsMemberCollectSpu> result = yytUmsMemberCollectSpuService.list(new LambdaQueryWrapper<YytUmsMemberCollectSpu>()
                .eq(YytUmsMemberCollectSpu::getMemberId, loginUser.getId()));
        return ResponseResult.success(result);
    }

    /**
     * 根据商品id列表获取商品详情列表
     */
    @PostMapping("/getInfoList")
    public ResponseResult getInfoList(@RequestBody List<String> skuIdList) {
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        return ResponseResult.success(yytPmsSkuInfoService.getInfoList(skuIdList, loginUser.getId()));
    }


    @PostMapping("/getMoreSpuCoupon")
    public ResponseResult getMoreSpuCoupon(@RequestBody List<String> skuIdList){
        List<YytSmsCoupon> result = new ArrayList<>();
        QueryWrapper<YytSmsCouponHistory> wrapper = new QueryWrapper<>();
        YytMember loginUser = LoginUtils.getLoginUser(redisService);
        wrapper.eq("member_id", loginUser.getId());
        wrapper.eq("use_type", 0);
        wrapper.eq("use_mode", 0);
        List<YytSmsCouponHistory> histories = yytSmsCouponHistoryService.list(wrapper);
        for (YytSmsCouponHistory history : histories) {
            QueryWrapper<YytSmsCouponSpuRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("coupon_id", history.getCouponId());
            List<YytSmsCouponSpuRelation> spuRelations = yytSmsCouponSpuRelationService.list(queryWrapper);
            if(spuRelations.size() >= skuIdList.size() ){
                boolean status = true;
                for (int i = 0; i < skuIdList.size(); i++) {
                    boolean isExist = false;
                    for (int j = 0; j < spuRelations.size(); j++) {
                        if(skuIdList.get(i).equals(spuRelations.get(j).getSpuId())) {
                            isExist = true;
                            break;
                        }
                    }
                    if (!isExist){
                        status = false;
                        break;
                    }
                }
                if (status){
                    YytSmsCoupon smsCoupon = yytSmsCouponService.getById(history.getCouponId());
                    smsCoupon.setBalance(history.getBalance());
                    smsCoupon.setHistoryId(history.getId());
                    result.add(smsCoupon);
                }else {
                    break;
                }
            }
        }
        return ResponseResult.success(result);
    }
}
